package Demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardList {
    private static final String[] SUITS = {"♦","♣","♥","♠"};//花色
    public static List<Card> buyCard(){
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 1; j <= 13; j++) {
           /* String suit = SUITS[i];
            int rank = j;*/
            Card card = new Card(SUITS[i],j);
            cardList.add(card);
            }
        }
        return cardList;
    }
    //洗牌算法
    public static void shuffle(List<Card> list){
        Random random = new Random();
        for (int i = list.size() - 1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(list,i,index);
        }
    }
    //交换牌
    private static void swap(List<Card> list,int i,int j){
        Card tmp = list.get(i);
        list.set(i,list.get(j));
        list.set(j,tmp);
    }
    public static void main(String[] args) {
        List<Card> list = buyCard();
        shuffle(list);
        //三个人轮流摸5张牌
        //使用二维数组存储
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        //i控制次数
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
            //每次都揭的是0下标的牌
                Card card = list.remove(0);
                hand.get(j).add(card);
            }
        }
        System.out.println("第一个人的牌:" + hand1);
        System.out.println("第二个人的牌:" + hand2);
        System.out.println("第三个人的牌:" + hand3);
        System.out.println("剩下的牌:" + list);
    }
}
